/*
 * Copyright 2006-2008 Martin Konzett <martin.konzett AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.toools.gwt.wmc.client.account.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import java.util.Collection;
import java.util.HashSet;
import org.toools.gwt.wmc.client.SimpleActionListener;
import org.toools.gwt.wmc.client.account.AccountService;

public class AccountPanel extends VerticalPanel {

    public static final String EVT_LOGGED_IN = "loggedin";

    private HorizontalPanel header = new HorizontalPanel();
    private HorizontalPanel headerleft = new HorizontalPanel();
    private HorizontalPanel bottom = new HorizontalPanel();

    private Collection<SimpleActionListener> listeners = new HashSet<SimpleActionListener>();

    TextBox email = new TextBox();
    PasswordTextBox pwd = new PasswordTextBox();
    Button loginb = null;
    Button registerb = null;

    public AccountPanel() {

        header.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        header.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
        headerleft.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
        headerleft.add( new HTML("Login or Register") );
        header.add(headerleft);
        header.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        this.add(header);

        email.setMaxLength(50);

        loginb = new Button("login");
        loginb.addClickListener( new LoginListener() );
        registerb = new Button("register");
        registerb.addClickListener( new RegisterListener() );

        this.add(email);
        this.add(pwd);
        this.add(loginb);
        this.add(registerb);
        bottom.setHorizontalAlignment( HasHorizontalAlignment.ALIGN_CENTER );
        bottom.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE );
        this.add( bottom );
        //this.add(comment);
    }

    public void addSimpleActionListener( SimpleActionListener pcl ) {
        this.listeners.add( pcl );
    }

    public void removeSimpleActionListener( SimpleActionListener pcl ) {
        this.listeners.remove( pcl );
    }

    private void fireLoginEvent( SimpleActionListener.SimpleActionEvent ae ) {
        for( SimpleActionListener pcl : listeners )
            pcl.actionPerformed( ae );
    }

    private class LoginListener implements ClickListener {

        public void onClick(Widget sender) {
            bottom.clear();
            AccountService.Util.getInstance().login( email.getText(), pwd.getText(), new AsyncCallback<Integer>() {

                public void onFailure(Throwable caught) {
                    error( caught.getMessage() );
                }

                public void onSuccess(Integer result) {
                    if( result != null ) {
                        bottom.clear();
                        bottom.add( new HTML( "Logged in!" ) );
                        fireLoginEvent( new SimpleLoginActionEvent( AccountPanel.this ) );
                    }
                    else
                        error( "Couldn't log in!" );
                }

                private void error( String msg ) {
                    bottom.clear();
                    bottom.add( new HTML( msg ) );
                }

            });
        }
    }

    private class RegisterListener implements ClickListener {

        public void onClick(Widget sender) {
            bottom.clear();
            AccountService.Util.getInstance().register( email.getText(), pwd.getText(), new AsyncCallback<Integer>() {

                public void onFailure(Throwable caught) {
                    error( caught.getMessage() );
                }

                public void onSuccess( Integer result) {
                    if( result != null ) {
                        bottom.clear();
                        bottom.add( new HTML( "Registered!" ) );
                    }
                    else {
                        error( "Couldn't register!");
                    }
                }

                private void error( String msg ) {
                    bottom.clear();
                    bottom.add( new HTML( msg ) );
                }
            });
        }
    }

    private class SimpleLoginActionEvent implements SimpleActionListener.SimpleActionEvent {

        private Object source;

        SimpleLoginActionEvent( Object source ) {
            this.source = source;
        }

        public Object getSource() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
}
